/*!
 * slideViewerPro 1.0
 * Examples and documentation at:
 * http://www.gcmingati.net/wordpress/wp-content/lab/jquery/svwt/
 * 2009 Gian Carlo Mingati
 * Version: 1.0.3 (29-MAY-2009)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 * Requires:
 * jQuery v1.3.2 or later
 * Option:
 * jQuery Timers plugin | plugins.jquery.com/project/timers (for autoslide mode)
 *
 */

jQuery.fn.slideViewerPro = function(settings) {
    settings = jQuery.extend({
        easeFunc: "easeInOutExpo",
        easeTime: 750,
        galBorderWidth: 6,
        galBorderColor: "#ff0000",
        thumbsPercentReduction: 12,
        thumbs: 5,
        thumbsTopMargin: 3,
        thumbsRightMargin: 3,
        thumbsBorderWidth: 3,
        thumbsBorderColor: "#d8d8d8",
        thumbsActiveBorderColor: "#ff0000",
        thumbsBorderOpacity: 1.0, // could be 0, 0.1 up to 1.0
        thumbsActiveBorderOpacity: 1.0, // could be 0, 0.1 up to 1.0
        autoslide: false,
        asTimer: 4000,
        buttonsWidth: 20,
        buttonsTextColor: "#ff0000",
        leftButtonInner: "-", //could be an image "<img src='images/larw.gif' />" or an escaped char as "&larr";
        rightButtonInner: "+", //could be an image or an escaped char as "&rarr";
        typo: false,
        navigator: "thumb",
        shuffle: false
    }, settings);


    $.standard = function(elem) {
        jQuery(function() {
            jQuery("div.svw").prepend("<img src='svwloader.gif' class='ldrgif' alt='loading...'/ >");
        });
        var j = 0;
        var quantofamo = 0;

        elem.each(function() {
            var container = jQuery(this);
            container.find("img.ldrgif").remove(); // removes the preloader gif
            container.removeClass("svw").addClass("stripViewer");
            var pictWidth = container.find("img").width();
            var pictHeight = container.find("img").height();
            var pictEls = container.find("li").size();
            var stripViewerWidth = pictWidth * pictEls;
            container.find("ul").css("width", stripViewerWidth); //assegnamo la larghezza alla lista UL
            container.css("width", pictWidth);
            container.css("height", pictHeight);
            var thumbsWidth = Math.round(pictWidth * settings.thumbsPercentReduction / 100);
            var thumbsHeight = Math.round(pictHeight * settings.thumbsPercentReduction / 100);
            var pos = 0;
            var r_enabled = true;
            var l_enabled = true;

            
            container.each(function(i) {
                if (settings.typo)
                {
                    jQuery(this).find("img").each(function(z) {
                        jQuery(this).after("<span class='typo'>" + jQuery(this).attr("alt") + "<\/span>");
                    });
                }

                jQuery(this).after("<div class='stripTransmitter' id='stripTransmitter" + (j) + "'><ul><\/ul><\/div>");
                jQuery(this).find("li").each(function(n) {
                    jQuery("div#stripTransmitter" + j + " ul").append("<li><a title='" + jQuery(this).find("img").attr("alt") + "' href='#'>" + (n + 1) + "<\/a><\/li>");
                });
                jQuery("div#stripTransmitter" + j + " a").each(function(z) {
                    jQuery(this).bind("click", function() {

                        jQuery(this).addClass("current").parent().parent().find("a").not(jQuery(this)).removeClass("current"); // wow!
                        var cnt = -(pictWidth * z);
                        container.find("ul").animate({ left: cnt}, settings.easeTime, settings.easeFunc);
                        return false;
                    });
                });


                // next image via image click	14/01/2009
                jQuery("div#stripTransmitter" + j + " a").parent().parent().parent().prev().find("img").each(function(z) {
                    jQuery(this).bind("click", function() {
                        var ui = jQuery(this).parent().parent().parent().next().find("a");
                        if (z + 1 < pictEls) {
                            ui.eq(z + 1).trigger("click");
                        }
                        else ui.eq(0).trigger("click");
                    });
                });


                jQuery("div#stripTransmitter" + j).css("width", pictWidth);
                jQuery("div#stripTransmitter" + j + " a:first").addClass("current");

                function authorityMixing()
                {
                    //right btt
                    (pos == pictEls - settings.thumbs) ? container.next().next().next().addClass("r_dis") : container.next().next().next().removeClass("r_dis");
                    (pos == pictEls - settings.thumbs) ? r_enabled = false : r_enabled = true;
                    //left btt
                    (pos == 0) ? container.next().next().addClass("l_dis") : container.next().next().removeClass("l_dis");
                    (pos == 0) ? l_enabled = false : l_enabled = true;
                }

                authorityMixing();

                if (settings.autoslide) {
                    var i = 1;
                    jQuery("div#stripTransmitter" + j).everyTime(settings.asTimer, "asld", function() {
                        jQuery(this).find("a").eq(i).trigger("click");
                        if (i == 0)
                        {
                            pos = 0;
                            l_enabled = false;
                            jQuery("div#stripTransmitter" + j).find("ul:not(:animated)").animate({ left: -(thumbsWidth + settings.thumbsRightMargin) * pos}, 500, settings.easeFunc, function() {
                                authorityMixing();
                            });
                        }
                        else l_enabled = true;

                        (i % settings.thumbs == 0) ? jQuery(this).next().next().trigger("click") : null;
                        (i < pictEls - 1) ? i++ : i = 0;
                    });

                    //stops autoslidemode
                    jQuery("a#right" + j).bind("mouseup", function() {
                        jQuery(this).prev().prev().stopTime("asld");
                    });
                    jQuery("a#left" + j).bind("mouseup", function() {
                        jQuery(this).prev().stopTime("asld");
                    });
                    jQuery("div#stripTransmitter" + j + " a").bind("mouseup", function() {
                        jQuery(this).parent().parent().parent().stopTime("asld");
                    });
                }

                if (settings.toolTip) {
                    container.next(".stripTransmitter ul").find("a").Tooltip({
                        track: true,
                        delay: 0,
                        showURL: false,
                        showBody: false
                    });
                }
            });
            j++;
        });
    }

    $.pro = function(elem) {
        jQuery.extend(jQuery.easing, // from the jquery.easing plugin
        {
            easeInOutExpo: function (x, t, b, c, d) {
                if (t == 0) return b;
                if (t == d) return b + c;
                if ((t /= d / 2) < 1) return c / 2 * Math.pow(2, 10 * (t - 1)) + b;
                return c / 2 * (-Math.pow(2, -10 * --t) + 2) + b;
            }
        });
        jQuery(function() {
            jQuery("div.svwp").prepend("<img src='images/svwloader.gif' class='ldrgif' alt='loading...'/ >"); //change with YOUR loader image path
        });
        var j = 0;

        elem.each(function() {
						function shuffle(a) {
					    var i = a.size();
					    while (--i) {
					        var j = Math.floor(Math.random() * (i));
					        var tmp = a.slice(i, i+1);
					        a.slice(j, j+1).insertAfter(tmp);
					    }
						} 
            var container = jQuery(this);
            (!settings.shuffle) ? null : shuffle(container.find("li"));
            container.find("img.ldrgif").remove();
            container.removeClass("svwp").addClass("slideViewer");
            container.attr("id", "svwp" + j);
            var pictWidth = container.find("img").attr("width");
            var pictHeight = container.find("img").attr("height");
            var pictEls = container.find("li").size();
            (pictEls >= settings.thumbs) ? null : settings.thumbs = pictEls;
            var slideViewerWidth = pictWidth * pictEls;
            var thumbsWidth = Math.round(pictWidth * settings.thumbsPercentReduction / 100);
            var thumbsHeight = Math.round(pictHeight * settings.thumbsPercentReduction / 100);
            var pos = 0;
            var r_enabled = true;
            var l_enabled = true;
            container.find("ul").css("width", slideViewerWidth).wrap(jQuery("<div style='width:" + pictWidth + "px; overflow: hidden; position: relative; top: 0; left: 0'>"));
            container.css("width", pictWidth);
            container.css("height", pictHeight);
            container.each(function(i) {
                if (settings.typo)
                {
                    jQuery(this).find("img").each(function(z) {
                        jQuery(this).after("<span class='typo'>" + jQuery(this).attr("alt") + "<\/span>");
                    });
                }
                jQuery(this).after("<div class='thumbSlider' id='thumbSlider" + j + "'><ul><\/ul><\/div>");
                jQuery(this).next().after("<a href='#' class='left' id='left" + j + "'><span>" + settings.leftButtonInner + "</span><\/a><a href='#' class='right' id='right" + j + "'><span>" + settings.rightButtonInner + "<\/span><\/a>");

                jQuery(this).find("li").each(function(n) {
                    jQuery("div#thumbSlider" + j + " ul").append("<li><a title='" + jQuery(this).find("img").attr("alt") + "' href='#'><img width='" + thumbsWidth + "' height='" + thumbsHeight + "' src='" + jQuery(this).find("img").attr("src") + "' /><p class='tmbrdr'>&nbsp;<\/p><\/a><\/li>");
                });
                jQuery("div#thumbSlider" + j + " a").each(function(z) {
                    jQuery(this).bind("click", function() {
                        jQuery(this).find("p.tmbrdr").css({borderColor: settings.thumbsActiveBorderColor, opacity: settings.thumbsActiveBorderOpacity});
                        jQuery(this).parent().parent().find("p.tmbrdr").not(jQuery(this).find("p.tmbrdr")).css({borderColor: settings.thumbsBorderColor, opacity: settings.thumbsBorderOpacity});
                        var cnt = -(pictWidth * z);
                        container.find("ul").animate({ left: cnt}, settings.easeTime, settings.easeFunc);
                        return false;
                    });
                });

                // right/left 'buttons'
                jQuery("a#right" + j).bind("click", function() {
                    if (r_enabled) (pictEls - pos > settings.thumbs * 2 || pictEls % settings.thumbs == 0) ? pos += settings.thumbs : pos += pictEls % settings.thumbs;
                    r_enabled = false;
                    jQuery(this).prev().prev().find("ul:not(:animated)").animate({ left: -(thumbsWidth + settings.thumbsRightMargin) * pos}, 500, settings.easeFunc, function() {
                        authorityMixing();
                    });
                    return false;
                });
                jQuery("a#left" + j).bind("click", function() {
                    if (l_enabled) (pictEls - pos > settings.thumbs || pictEls % settings.thumbs == 0) ? pos -= settings.thumbs : pos -= pictEls % settings.thumbs;
                    l_enabled = false;
                    jQuery(this).prev().find("ul:not(:animated)").animate({ left: -(thumbsWidth + settings.thumbsRightMargin) * pos}, 500, settings.easeFunc, function() {
                        authorityMixing();
                    });
                    return false;
                });



                //CSS	defs @ runtime
                var tBorder = settings.thumbsBorderWidth;
                var contBorder = settings.galBorderWidth

                jQuery(".slideViewer a img").css({border: "0"});
                jQuery("div#svwp" + j + " span.typo").css({position: "absolute", margin: "0 0 0 " + (-pictWidth) + "px"});
                jQuery("div#svwp" + j).css({border: settings.galBorderWidth + "px solid " + settings.galBorderColor});

                jQuery("div#thumbSlider" + j).css({position: "relative", left: contBorder, top: settings.thumbsTopMargin + "px", width: settings.thumbs * thumbsWidth + ((settings.thumbsRightMargin * settings.thumbs) - settings.thumbsRightMargin), height: thumbsHeight, textAlign: "center", overflow: "hidden", margin: "0 auto"});
                jQuery("div#thumbSlider" + j + " ul").css({width: (thumbsWidth * pictEls) + settings.thumbsRightMargin * pictEls, position: "relative", left: "0", top: "0"});
                jQuery("div#thumbSlider" + j + " ul li").css({marginRight: settings.thumbsRightMargin});

                jQuery("div#thumbSlider" + j).find("p.tmbrdr").css({width: (thumbsWidth - tBorder * 2) + "px", height: (thumbsHeight - tBorder * 2) + "px", top: -(thumbsHeight) + "px", border: settings.thumbsBorderWidth + "px solid " + settings.thumbsBorderColor, opacity: settings.thumbsBorderOpacity});
                jQuery("div#thumbSlider" + j + " a:first p.tmbrdr").css({borderColor: settings.thumbsActiveBorderColor, opacity: settings.thumbsActiveBorderOpacity});

                var rbttLeftMargin = (pictWidth / 2) + (jQuery("div#thumbSlider" + j).width() / 2) + settings.thumbsRightMargin + contBorder;
                var lbttLeftMargin = (pictWidth / 2) - (jQuery("div#thumbSlider" + j).width() / 2) - (settings.buttonsWidth + settings.thumbsRightMargin) + contBorder;
                var innerImgH = jQuery("a#right" + j + " span img").attr("height");

                jQuery("a#left" + j).css({display: "block", textAlign: "center", width: settings.buttonsWidth + "px" , height: thumbsHeight + "px",  margin: -(thumbsHeight - settings.thumbsTopMargin) + "px 0 0 " + lbttLeftMargin + "px", textDecoration: "none", lineHeight: thumbsHeight + "px", color: settings.buttonsTextColor});
                jQuery("a#right" + j).css({display: "block", textAlign: "center", width: settings.buttonsWidth + "px", height: thumbsHeight + "px" , margin: -(thumbsHeight) + "px 0 0 " + rbttLeftMargin + "px", textDecoration: "none", lineHeight: thumbsHeight + "px", color: settings.buttonsTextColor});
                jQuery("a#left" + j + " span img").css({margin: Math.round((thumbsHeight / 2) - (innerImgH / 2)) + "px 0 0 0"});
                jQuery("a#right" + j + " span img").css({margin: Math.round((thumbsHeight / 2) - (innerImgH / 2)) + "px 0 0 0"});

                function authorityMixing()
                {
                    //right btt
                    (pos == pictEls - settings.thumbs) ? container.next().next().next().addClass("r_dis") : container.next().next().next().removeClass("r_dis");
                    (pos == pictEls - settings.thumbs) ? r_enabled = false : r_enabled = true;
                    //left btt
                    (pos == 0) ? container.next().next().addClass("l_dis") : container.next().next().removeClass("l_dis");
                    (pos == 0) ? l_enabled = false : l_enabled = true;
                }

                authorityMixing();

                if (settings.autoslide) {
                    var i = 1;
                    jQuery("div#thumbSlider" + j).everyTime(settings.asTimer, "asld", function() {
                        jQuery(this).find("a").eq(i).trigger("click");
                        if (i == 0)
                        {
                            pos = 0;
                            l_enabled = false;
                            jQuery("div#thumbSlider" + j).find("ul:not(:animated)").animate({ left: -(thumbsWidth + settings.thumbsRightMargin) * pos}, 500, settings.easeFunc, function() {
                                authorityMixing();
                            });
                        }
                        else l_enabled = true;

                        (i % settings.thumbs == 0) ? jQuery(this).next().next().trigger("click") : null;
                        (i < pictEls - 1) ? i++ : i = 0;
                    });

                    //stops autoslidemode
                    jQuery("a#right" + j).bind("mouseup", function() {
                        jQuery(this).prev().prev().stopTime("asld");
                    });
                    jQuery("a#left" + j).bind("mouseup", function() {
                        jQuery(this).prev().stopTime("asld");
                    });
                    jQuery("div#thumbSlider" + j + " a").bind("mouseup", function() {
                        jQuery(this).parent().parent().parent().stopTime("asld");
                    });
                }

                jQuery("div#svwp" + j + ", div#thumbSlider" + j + ", a#left" + j + ", a#right" + j).wrapAll("<div style='width:" + pictWidth + "px'><\/div>");
            });
            (jQuery("div#thumbSlider" + j).width() + (settings.buttonsWidth * 2) >= pictWidth) ? alert("ALERT: THE THUMBNAILS SLIDER IS TOO WIDE! \nthumbsPercentReduction and/or buttonsWidth needs to be scaled down!") : null;
            j++;
        });
    }

    if (settings.navigator == 'text') {
        $.standard(this);
    } else {
        $.pro(this);
    }
};